package com.lyeducation.sfexpressutil;

import com.lyeducation.util.controllerUtil.JsonResult;
import com.sf.openapi.common.entity.AppInfo;
import com.sf.openapi.common.entity.HeadMessageReq;
import com.sf.openapi.common.entity.MessageReq;
import com.sf.openapi.common.entity.MessageResp;
import com.sf.openapi.express.sample.order.dto.OrderQueryReqDto;
import com.sf.openapi.express.sample.order.dto.OrderQueryRespDto;
import com.sf.openapi.express.sample.order.dto.OrderReqDto;
import com.sf.openapi.express.sample.order.dto.OrderRespDto;
import com.sf.openapi.express.sample.order.tools.OrderTools;
import com.sf.openapi.express.sample.security.dto.TokenRespDto;
import com.sf.openapi.express.sample.security.tools.SecurityTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

/**
 * 顺丰速递工具类
 * 參考官網demo https://open.sf-express.com/apitools/sdk.html
 */
@Component
public class SFExpressUtil {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private SfExpressConfig sfConfig;

    public HeadMessageReq createHeadMessageReq(int transType,
                                               String transMessageId) {
        HeadMessageReq headMessageReq = new HeadMessageReq();
        headMessageReq.setTransType(transType);
        headMessageReq.setTransMessageId(transMessageId);
        return headMessageReq;
    }

    /**
     * 获取token
     *
     * @return
     * @throws Exception
     */
    public MessageResp<TokenRespDto> getAccessToken() throws Exception {
        AppInfo appInfo = new SFExpressAppInfo();
        MessageReq accessTokenReq = new MessageReq();
        HeadMessageReq headMessageReq = createHeadMessageReq(SFCodeEnum.ACCESS_TOKEN查询.getCode(), null);
        accessTokenReq.setHead(headMessageReq);
        MessageResp<TokenRespDto> tokenRespMessage = SecurityTools.applyAccessToken(sfConfig.getAccessTokenUrl(), appInfo, accessTokenReq);
        return tokenRespMessage;
    }

    /**
     * @param token
     * @param refreshToken
     */
    public void saveTokenRefreshToken(String token, String refreshToken) {
        redisTemplate.opsForHash().put("sf", "sfToken", token);
        redisTemplate.opsForHash().put("sf", "sfRefreshToken", refreshToken);
    }

    /**
     * @param tokenOrRefreshToken true 获取token false refreshToken
     * @return
     */
    public String getTokenOrRefreshToken(boolean tokenOrRefreshToken) {
        if (tokenOrRefreshToken) {
            return getToken();
        }
        return getRefreshToken();
    }


    public String getToken() {
        Object sfToken = redisTemplate.opsForHash().get("sf", "sfToken");
        if (sfToken != null) {
            return sfToken.toString();
        }
        return null;
    }


    public String getRefreshToken() {
        Object sfRefreshToken = redisTemplate.opsForHash().get("sf", "sfRefreshToken");
        if (sfRefreshToken != null) {
            return sfRefreshToken.toString();
        }
        return null;
    }

    /**
     * 刷新
     *
     * @return
     * @throws Exception
     */
    public MessageResp<TokenRespDto> refreshToken() throws Exception {
        AppInfo appInfo = new SFExpressAppInfo();
        appInfo.setAccessToken(getTokenOrRefreshToken(true));
        appInfo.setRefreshToken(getTokenOrRefreshToken(false));
        MessageReq accessTokenReq = new MessageReq();
        HeadMessageReq headMessageReq = createHeadMessageReq(SFCodeEnum.ACCESS_TOKEN查询.getCode(), null);
        accessTokenReq.setHead(headMessageReq);
        MessageResp<TokenRespDto> apply = SecurityTools.refreshAccessToken(sfConfig.getAccessTokenUrl(), appInfo, accessTokenReq);
        return apply;
    }

    /**
     * 下單
     *
     * @param orderReqMessage MessageReq<OrderReqDto>包含需要传入的俩个实体
     *                        1：OrderReqDto为下单所需参数 2：HeadMessageReq
     * @return
     */
    public MessageResp<OrderRespDto> order(MessageReq<OrderReqDto> orderReqMessage) {
        AppInfo appInfo = new SFExpressAppInfo();
        appInfo.setAccessToken(getTokenOrRefreshToken(true));
//        MessageReq<OrderReqDto> orderReq = orderReqHasToken;
//        HeadMessageReq headMessageReq = createHeadMessageReq(SFCodeEnum.订单交易.getCode(), orderReqHasToken.getTransMessageId());
//        orderReq.setHead(headMessageReq);
        orderReqMessage.getHead().setTransType(SFCodeEnum.订单交易.getCode());
        MessageResp<OrderRespDto> orderResp = null;
        try {
            orderResp = OrderTools.order(sfConfig.getOrderUrl(), appInfo, orderReqMessage);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return orderResp;
    }

    /**
     * 订单查询
     *
     * @param orderQueryReqMessage
     * @return
     */
    public MessageResp<OrderQueryRespDto> orderQuery(MessageReq<OrderQueryReqDto> orderQueryReqMessage) {
        AppInfo appInfo = new SFExpressAppInfo();
        appInfo.setAccessToken(getTokenOrRefreshToken(true));
//        HeadMessageReq headMessageReq = createHeadMessageReq(SFCodeEnum.订单结果查询.getCode(), orderReqHasToken.getTransMessageId());
        orderQueryReqMessage.getHead().setTransType(SFCodeEnum.订单结果查询.getCode());
        MessageResp<OrderQueryRespDto> orderQueryRespDto = null;
        try {
            orderQueryRespDto = OrderTools.orderQuery(sfConfig.getOrderQueryUrl(), appInfo, orderQueryReqMessage);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return orderQueryRespDto;
    }

    public JsonResult sfCallBackAccessToken() throws Exception {
        return null;
    }

}
